clear all
//set maxvar 120000
cd "C:\Users\mrabier\Dropbox\JOLE WARN Act\analysis/data/built"
use "cps_built_final_sample.dta", clear
cd "C:\Users\mrabier\Dropbox\JOLE WARN Act\analysis/output/"

* 0) packages
* install missing deps used under the hood
cap which avar
if _rc ssc install avar

cap which reghdfe
if _rc ssc install reghdfe

cap which ftools
if _rc ssc install ftools

cap which ivreg2
if _rc ssc install ivreg2

cap which event_plot
if _rc ssc install event_plot

cap which pretrends
if _rc ssc install pretrends

* 1) cohort year and controls
bysort STATEFIP (LOSSYEAR): egen w = min(cond(POSTTREAT==1, LOSSYEAR, .))
gen never_treat = missing(w)

* 2) relative time
gen rel = LOSSYEAR - w

* 3) choose window as positive counts
local max_post = 10
local min_pre  = 10

* if re-running
capture drop L_* F_*

* 4) build lag dummies L_0..L_max_post and a top bin
forvalues j = 0/`max_post' {
    gen byte L_`j' = (rel == `j')
}
replace L_`max_post' = (rel >= `max_post')

* 5) build lead dummies F_1..F_min_pre and a bottom bin
forvalues j = 1/`min_pre' {
    gen byte F_`j' = (rel == -`j')
}
replace F_`min_pre' = (rel <= -`min_pre')

* 7) ensure never-treated contribute to no bins
foreach v of varlist L_* F_* {
    replace `v' = 0 if never_treat
}

* 8) choose a reference period to omit (common choice: F_1, the year just before treatment)
drop F_1

* 9) assemble varlist in order (note F_1 omitted on purpose)
local esvars
forvalues j = `min_pre'(-1)2 {
    local esvars `esvars' F_`j'
}
forvalues j = 0/`max_post' {
    local esvars `esvars' L_`j'
}

* 7) run Sun and Abraham with FE set

eventstudyinteract NOTICELEN `esvars' FULLTIME UNION YEARS HI MARITAL EDUC AGE DWWEEKLLOG NCHILDBIN GDPPERCAPITA UNEMPRATE EMPPOP, ///
    absorb(STATEFIP LOSSYEAR DWOCC1990) ///
    cohort(w) control_cohort(never_treat) ///
    vce(cluster STATEFIP)

************Figure 1********************

event_plot e(b_iw)#e(V_iw), stub_lead(F_#) stub_lag(L_#) ///
    plottype(scatter) /// Use scatter plot for point estimates
    ciplottype(rcap) /// Keep confidence intervals
    alpha(0.05) /// Set 95% confidence intervals
    lag_opt(msymbol(diamond) mfcolor(white) mlcolor(black)) /// Diamonds for lags
    lead_opt(msymbol(triangle) mfcolor(white) mlcolor(black)) /// Triangles for leads
    graph_opt(title("Effect of state WARN on Probability of ≥ 30 Days Notice") ///
              xtitle("Years since law active") ytitle("Percentage Point Change") ///
              xline(0, lcolor(black) lpattern(dash)) /// Reference line at treatment
              yline(-1(1)4, lcolor(gs10) lpattern(solid))) // Keep horizontal grid lines
	
	
* 9) Roth-style display using pretrends
matrix b = e(b_iw)
matrix V = e(V_iw)
//matrix l_vec = 0.5 \ 0.5 \ 0 

pretrends power 0.8, pre(1/4) post(5/10)
*Slope for 50% power =  .019
*Slope for 80% power =  .029645

